package com.ruizhong.centercontroll.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruizhong.centercontroll.dao.TScheduledTaskMapper;
import com.ruizhong.centercontroll.entity.TCommand;
import com.ruizhong.centercontroll.entity.TScheduledTask;
import com.ruizhong.centercontroll.service.TCommandService;
import com.ruizhong.centercontroll.service.TScheduledTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

@Service
public class TScheduledTaskServiceImpl extends ServiceImpl<TScheduledTaskMapper, TScheduledTask> implements TScheduledTaskService {

/**@Description: 根据定时任务ID修改定时任务
 * @param
 * @return
 * @version v1.0
 * @author fengweijie
 * @date 2024/12/6 15:31
 */
    @Override
    @Transactional
    public void updateScheduledTaskById(TScheduledTask tScheduledTask) {
        Boolean needRepeat = tScheduledTask.getNeedRepeat();
        if (needRepeat) {
            List<String> repeatGroup = tScheduledTask.getRepeatGroup();
            StringBuilder repeatGroupStr = new StringBuilder();
            for (String s : repeatGroup) {
                repeatGroupStr.append(s + "#");
            }
            tScheduledTask.setRepeatGroupData(repeatGroupStr.toString());
        }
        tScheduledTask.setNeedRepeatData(needRepeat ? 1 : 0);
        updateById(tScheduledTask);
        HashSet<Integer> ids = new HashSet<>();
        ids.add(tScheduledTask.getId());
        tCommandService.delCommandsByMain(ids, "SCHEDULE");
        List<TCommand> commandList = tScheduledTask.getCommandList();
        for (int i = 0; i < commandList.size(); i++) {
            TCommand tCommand = commandList.get(i);
            tCommand.setMainType("SCHEDULE");
            tCommand.setMainId(tScheduledTask.getId());
            tCommand.setCommandEvent("schedule");
            tCommand.setCommandIndex(i);
            tCommand.setGetReturnData(0);
            Boolean hexadecimal = tCommand.getHexadecimal();
            tCommand.setHexadecimalData(hexadecimal == null ? 0 : hexadecimal ? 1 : 0);
        }
        tCommandService.saveCommandList(commandList);
    }

    /**@Description: 根据ID删除任务
     * @param
     * @return
     * @version v1.0
     * @author fengweijie
     * @date 2024/12/6 15:32
     */
    @Override
    @Transactional
    public void delTaskById(Integer id) {
        removeById(id);
        HashSet<Integer> ids = new HashSet<>();
        ids.add(id);
        tCommandService.delCommandsByMain(ids, "SCHEDULE");
    }

    /**@Description: TODO methods
     * @param
     * @return
     * @version v1.0
     * @author fengweijie
     * @date 2024/12/6 15:32
     */
    @Override
    public void sync() {
//        List<TScheduledTask> list = list();
//        ArrayList<TCommand> commands = new ArrayList<>();
//        for (int i = 0; i < list.size(); i++) {
//            TScheduledTask tScheduledTask = list.get(i);
//            TCommand tCommand = new TCommand();
//            tCommand.setMainType("SCHEDULE");
//            tCommand.setMainId(tScheduledTask.getId());
//            tCommand.setCommandEvent("schedule");
//            tCommand.setCommandIndex(0);
//            tCommand.setGetReturnData(0);
//            tCommand.setEventType(tScheduledTask.getEventType());
//            tCommand.setSonEventType(tScheduledTask.getSonEventType());
//            tCommand.setIpAddress(tScheduledTask.getIpAddress());
//            tCommand.setPort(tScheduledTask.getPort());
//            tCommand.setInstructionData(tScheduledTask.getInstructionData());
//            Boolean hexadecimal = tCommand.getHexadecimal();
//            tCommand.setHexadecimalData(hexadecimal == null ? 0 : hexadecimal ? 1 : 0);
//            commands.add(tCommand);
//        }
//        tCommandService.saveCommandList(commands);
    }

    @Autowired
    private TCommandService tCommandService;

/**@Description: 保存新的定时任务
 * @param
 * @return
 * @version v1.0
 * @author fengweijie
 * @date 2024/12/6 15:34
 */
    @Override
    @Transactional
    public void saveNewScheduledTask(TScheduledTask tScheduledTask) {
        Boolean needRepeat = tScheduledTask.getNeedRepeat();
        if (needRepeat != null && needRepeat) {
            tScheduledTask.setExecuteDate(null);
            List<String> repeatGroup = tScheduledTask.getRepeatGroup();
            StringBuilder repeatGroupStr = new StringBuilder();
            for (String s : repeatGroup) {
                repeatGroupStr.append(s + "#");
            }
            tScheduledTask.setRepeatGroupData(repeatGroupStr.toString());
        }
        tScheduledTask.setNeedRepeatData(needRepeat == null ? 0 : needRepeat ? 1 : 0);
        save(tScheduledTask);
        List<TCommand> commandList = tScheduledTask.getCommandList();
        for (int i = 0; i < commandList.size(); i++) {
            TCommand tCommand = commandList.get(i);
            tCommand.setMainType("SCHEDULE");
            tCommand.setMainId(tScheduledTask.getId());
            tCommand.setCommandEvent("schedule");
            tCommand.setCommandIndex(i);
            tCommand.setGetReturnData(0);
            Boolean hexadecimal = tCommand.getHexadecimal();
            tCommand.setHexadecimalData(hexadecimal == null ? 0 : hexadecimal ? 1 : 0);
        }
        tCommandService.saveCommandList(commandList);
    }

    /**@Description: 获取当前系统所有定时任务
     * @param
     * @return
     * @version v1.0
     * @author fengweijie
     * @date 2024/12/6 15:34
     */
    @Override
    public List<TScheduledTask> getAllScheduledTask() {
        List<TScheduledTask> list = list();
        for (TScheduledTask tScheduledTask : list) {
            Integer needRepeatData = tScheduledTask.getNeedRepeatData();
            tScheduledTask.setNeedRepeat(needRepeatData == null ? false : needRepeatData == 1);
            if (tScheduledTask.getNeedRepeat()) {
                String repeatGroupData = tScheduledTask.getRepeatGroupData();
                String[] repeatDate = repeatGroupData.split("#");
                ArrayList<String> repeatDateArr = new ArrayList<>();
                for (String s : repeatDate) {
                    repeatDateArr.add(s);
                }
                tScheduledTask.setRepeatGroup(repeatDateArr);
            } else {
                tScheduledTask.setRepeatGroup(Collections.EMPTY_LIST);
            }
            Integer hexadecimalData = tScheduledTask.getHexadecimalData();

            tScheduledTask.setHexadecimal(hexadecimalData == null ? false : hexadecimalData == 1);

            List<TCommand> commandListByMain = tCommandService.getCommandListByMain(tScheduledTask.getId(), "SCHEDULE", "schedule");
            for (TCommand tCommand : commandListByMain) {
                tCommand.setHexadecimal(tCommand.getHexadecimalData() == 1);
            }
            tScheduledTask.setCommandList(commandListByMain);
        }
//        list.stream().map(TScheduledTask::getId)

        return list;
    }


}
